﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Lincolnwood.Foundation.Common.Data
{
    public static class Cloner
    {
        public static IEnumerable<T> Clone<T>(IEnumerable<T> enumerable)
        {
            if (enumerable != null)
            {
                ICollection<T> collection = new Collection<T>();

                foreach (var item in enumerable)
                {
                    collection.Add(Cloner.Clone<T>(item));
                }

                return collection;
            }
            return null;
        }

        public static T Clone<T>(T item)
        {
            if (item != null)
            {
                MemoryStream s = new MemoryStream();
                BinaryFormatter f = new BinaryFormatter();

                f.Serialize(s, item);
                s.Position = 0;
                T clone = (T)f.Deserialize(s);
                return clone;
            }

            return default(T);

        }
    }
}
